package com.zhongyu.controller;
import com.zhongyu.pojo.Kehu;
import com.zhongyu.service.KehuService;
import com.zhongyu.vo.KehuVo;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
/*
客户
 */
@RestController
@RequestMapping("/jibenxinxi/kehu")
public class KehuController {

    @Resource
    private KehuService kehuService;


    @GetMapping("/search/{pageNo}/{pageSize}")
    public ResultVo search(@PathVariable Integer pageNo, @PathVariable Integer pageSize, KehuVo kehuVo){
        PageResult pageResult=kehuService.searchAll(pageNo,pageSize,kehuVo);
        return ResultVo.success(pageResult);
    }
    @GetMapping("/list")
    public ResultVo list( ){
        List<Kehu> list =kehuService.searchAll();
        return ResultVo.success(list);

    }

    @PostMapping("/add")
    @RequiresPermissions("kehu:add")
    public ResultVo add(@RequestBody Kehu kehu){
        try{
                kehuService.add(kehu);
            return ResultVo.success("添加客户成功");
        }catch (Exception ex){
            return ResultVo.failure("添加客户失败",ex);
        }
    }



    @GetMapping("/del")
    public ResultVo del(Integer id) {
        try {
            kehuService.del(id);
            return ResultVo.success("删除客户成功!");
        } catch (Exception ex) {

            return ResultVo.failure("删除客户失败！", ex);
        }
    }

    @PostMapping("/update")
    @RequiresPermissions("kehu:update")
    public ResultVo update(@RequestBody Kehu kehu ){
        try {
            kehuService.update(kehu);
            return  ResultVo.success("修改客户成功");
        }catch (Exception ex){
            return  ResultVo.failure("修改客户失败",ex);
        }

    }
}
